#include <iostream>
#include <queue>
#include <mutex>
#include <thread>
#include <chrono>

using namespace std;

struct Data {
    int a;
};

std::queue<struct Data> g_queue;
std::mutex g_lock;

bool sub_exit = false;

void test_thread(void){
    while(!sub_exit) {
        {
            std::lock_guard<std::mutex> guard(g_lock);
            if(g_queue.size() > 0){
                Data data = g_queue.front();
                std::cout << "test_thread: a: " << data.a << endl;
                g_queue.pop();
            }
        }
        std::this_thread::sleep_for(std::chrono::microseconds(5));
    }
}

int main(int argc, const char *argv[]) {
    std::thread thread(test_thread);
    for(int i = 0;i < 500; i++) {
        Data data;
        data.a = i;
        {
            std::lock_guard<std::mutex> guard(g_lock);
            g_queue.push(data);
            std::cout << "main thread: add a: " << data.a << endl;
        }
        std::this_thread::sleep_for(std::chrono::microseconds(100));
    }

    sub_exit = true;
    thread.join();

    return 0;
}